package com.jade.cloud.store.service.impl;

import com.jade.bss.DbException;
import com.jade.bss.base.utils.CopyBean;
import com.jade.bss.db.impl.BaseFacadeImpl;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.framework.base.util.PagingResult;
import com.jade.zsy.store.dao.EmployeeKpiDao;
import com.jade.zsy.store.entity.EmployeeKpi;
import com.jade.zsy.store.service.EmployeeKpiFacade;
import com.jade.zsy.store.vo.EmployeeKpiVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @version 1.0--Created by wq on 2017/7/1.
 * @Description:
 */
@Service("employeeKpiFacadeImpl")
public class EmployeeKpiFacadeImpl extends BaseFacadeImpl implements EmployeeKpiFacade {

    @Autowired
    private EmployeeKpiDao employeeKpiDao;

    @Override
    @Transactional
    public void save(Object entity) throws DbException{
        EmployeeKpiVo vo = (EmployeeKpiVo)entity;
        try {
            EmployeeKpi employee= CopyBean.copy(vo,EmployeeKpi.class);
            super.save(employee);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

    }

    @Transactional
    public void delete(long... ids) throws DbException {
        super.delete(EmployeeKpi.class,ids);
    }

    @Transactional
    public void update(EmployeeKpiVo employeeKpiVo)throws DbException,IllegalAccessException,InstantiationException{
        EmployeeKpi employeeKpi = CopyBean.copy(employeeKpiVo,EmployeeKpi.class);
        super.update(employeeKpi);
    }
    @Transactional
    public void disabled(long... ids) throws DbException {
        for (long id : ids) {
            EmployeeKpi employeeKpi =super.getById(EmployeeKpi.class,id);
            employeeKpi.setStatus(true);
            super.update(employeeKpi);
        }
    }
    @Transactional
    public void enabled(long... ids) throws DbException {
        for (long id : ids) {
            EmployeeKpi employeeKpi =super.getById(EmployeeKpi.class,id);
            employeeKpi.setStatus(false);
            super.update(employeeKpi);
        }
    }

    public EmployeeKpiVo query(long employeeId)throws DbException,IllegalAccessException,InstantiationException{
        PagingList<EmployeeKpi> list =  super.queryEntity(EmployeeKpi.class,new Criteria().add(Restrictions.eq("employeeId",employeeId)).
                add(Restrictions.eq("status",true)));
        if(!list.isEmpty()) {
            List<EmployeeKpiVo> list1 = CopyBean.copy(list,EmployeeKpiVo.class);
            return list1.get(0);
        }
        return null;
    }

    public PagingResult queryEntity(Object entity, int page, int size, String keys)
            throws DbException, IllegalAccessException, InstantiationException{
        EmployeeKpiVo employeeKpiVo = (EmployeeKpiVo)entity;
        Criteria criteria = new Criteria((page - 1) * size, size);
        PagingList<EmployeeKpi> list = employeeKpiDao.queryEntity(EmployeeKpi.class,criteria);
        PagingResult pagingResult = PagingResult.getResult(CopyBean.copy(list,employeeKpiVo.getClass()),list.getTotalCount(),page,size);
        return pagingResult;
    }
}
